package userUI;

import InformationUI.Uesr;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class HumanUi extends JFrame {
    private JTextField searchField;
    private JButton searchButton;
    private JButton addButton;
    private JTable employeeTable;
    private DefaultTableModel tableModel;


    public HumanUi() {
        // 设置窗口标题
        setTitle("员工信息管理系统");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建主面板并设置布局
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 第一行：输入框、搜索按钮、添加按钮
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        searchField = new JTextField(20);
        searchButton = new JButton("搜索");
        addButton = new JButton("添加");

        // 为搜索按钮添加事件监听器
        searchButton.addActionListener(e -> {
            String searchText = searchField.getText().trim();
            if (!searchText.isEmpty()) {
                filterTable(searchText); // 调用筛选方法
            } else {
                JOptionPane.showMessageDialog(null, "请输入搜索内容", "提示", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 为添加按钮添加事件监听器
        addButton.addActionListener(e -> {
            String[] options = {"确定", "取消"};
            String[] columnNames = {"ID", "姓名","性别","电话", "职位", "薪水","部门"};
            Object[] inputs = new Object[columnNames.length];
            int result = JOptionPane.showOptionDialog(
                null,
                inputs,
                "添加新员工",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                null,
                options,
                options[0]
            );

            if (result == JOptionPane.OK_OPTION) {
                // 验证输入是否为空
                boolean isValid = true;
                for (Object input : inputs) {
                    if (input == null || ((String) input).trim().isEmpty()) {
                        isValid = false;
                        break;
                    }
                }

                if (isValid) {
                    // 将新员工信息添加到表格
                    tableModel.addRow(inputs);
                } else {
                    JOptionPane.showMessageDialog(null, "所有字段都必须填写", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        topPanel.add(searchField);
        topPanel.add(searchButton);
        topPanel.add(addButton);

        // 中间：表格
        tableModel = new DefaultTableModel(
                new Object[][]{},
                new String[]{"ID", "姓名","性别","电话", "职位", "薪水","部门"}
        );
        employeeTable = new JTable(tableModel);

        // 添加20个空行
        for (int i = 0; i < 20; i++) {
            tableModel.addRow(new Object[]{i+1, "", "", "","","",""});
        }

        // 为表格添加右键菜单
        addPopupMenuToTable();

        // 将表格放入滚动面板
        JScrollPane scrollPane = new JScrollPane(employeeTable);

        // 组装主面板
        mainPanel.add(topPanel, BorderLayout.NORTH);
        mainPanel.add(scrollPane, BorderLayout.CENTER);

        // 将主面板添加到窗口
        add(mainPanel);

        // 显示窗口
        setVisible(true);
    }

    // 新增方法：筛选表格数据
    private void filterTable(String searchText) {
        DefaultTableModel model = (DefaultTableModel) employeeTable.getModel();
        TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<>(model);
        employeeTable.setRowSorter(sorter);
        sorter.setRowFilter(RowFilter.regexFilter("(?i)" + searchText)); // 忽略大小写匹配
    }

    private void addPopupMenuToTable() {
        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem editItem = new JMenuItem("修改");
        JMenuItem deleteItem = new JMenuItem("删除");

        popupMenu.add(editItem);
        popupMenu.add(deleteItem);

        employeeTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showPopupMenu(e);
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showPopupMenu(e);
                }
            }

            private void showPopupMenu(MouseEvent e) {
                int row = employeeTable.rowAtPoint(e.getPoint());
                if (row >= 0 && row < employeeTable.getRowCount()) {
                    employeeTable.setRowSelectionInterval(row, row);
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        // 添加编辑菜单项的事件监听器
        editItem.addActionListener(e -> {
            int selectedRow = employeeTable.getSelectedRow();
            if (selectedRow >= 0) {
                String employeeId = (String) tableModel.getValueAt(selectedRow, 0); // 获取选中行的ID
                JOptionPane.showMessageDialog(null, "编辑员工ID: " + employeeId, "编辑提示", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 添加删除菜单项的事件监听器
        deleteItem.addActionListener(e -> {
            int selectedRow = employeeTable.getSelectedRow();
            if (selectedRow >= 0) {
                String employeeId = (String) tableModel.getValueAt(selectedRow, 0); // 获取选中行的ID
                JOptionPane.showMessageDialog(null, "删除员工ID: " + employeeId, "删除提示", JOptionPane.WARNING_MESSAGE);
            }
        });
    }
}